package tamasle.insa.ecoconduite;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import tamasle.insa.ecoconduite.route.Road;

/**
 * Created by Aurelien on 05/05/2015.
 */
public class RoadAdapter extends BaseAdapter{

    private ArrayList<Road> data;
    private LayoutInflater inflater;

    public RoadAdapter(Context context,ArrayList list){
        data=list;
        inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount()
    {
        // getCount() represents how many items are in the list
        return data.size();
    }

    @Override
    // get the data of an item from a specific position
    // i represents the position of the item in the list
    public Object getItem(int i)
    {
        return data.get(i);
    }

    @Override
    // get the position id of the item from the list
    public long getItemId(int i)
    {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup)
    {

        // check to see if the reused view is null or not, if is not null then
        // reuse it
        if (view == null)
        {
            view =inflater.inflate(R.layout.list_item, null);
        }

        // get the string item from the position "position" from array list to
        // put it on the TextView
        Road r= data.get(position);
        if (r != null)
        {

            TextView roadName = (TextView) view.findViewById(R.id.list_item_text_view);

            if (roadName != null)
            {
                // set the item name on the TextView
                roadName.setText(r.getName());
            }
        }

        // this method must return the view corresponding to the data at the
        // specified position.
        return view;

    }
}
